#include "DMA_HAL.h"
#include "i2c.h"
#include <string.h>
#include "OLEDFont_DMA.h"
#define I2C_MEMADD_SIZE_8BIT 0x00000001U

static unsigned char OLED_Buffer[4][1024];
unsigned char InitCMD[]={
  	0xAE,   //display off

	0x20,	//Set Memory Addressing Mode	
	0x00,	//00,Horizontal Addressing Mode,01,Vertical Addressing Mode,10,Page Addressing Mode (RESET,,11,Invalid

//	0xb0,	//Set Page Start Address for Page Addressing Mode,0-7

	0x21,	//Set begin&end column
	0x00,	//begin column=0
	0x7f,	//end column=127

	0x22,   //Set begin&end Page
	0x00,	//begin Page=0
	0x07,	//end Page=7

	0xc8,   //Set COM Output Scan Direction
	0x00,   //---set low column address
	0x10,   //---set high column address
	0x40,   //--set start line address

	0x81,   //--set contrast control register
	0xff,   //亮度调节 0x00~0xff
	0xa1,   //--set segment re-map 0 to 127
	0xa6,   //--set normal display
	0xa8,   //--set multiplex ratio 1 to 64,
	0x3F,   //
	0xa4,   //0xa4,Output follows RAM content,0xa5,Output ignores RAM content
	0xd3,   //-set display offset
	0x00,   //-not offset
	0xd5,   //--set display clock divide ratio/oscillator frequency
	0xf0,   //--set divide ratio
	0xd9,   //--set pre-charge period
	0xda,   //--set com pins hardware configuration
	0x12,
	0xdb,   //--set vcomh
	0x20,   //0x20,0.77xVcc
	0x8d,   //--set DC-DC enable
	0x14,   //
	0xaf,   //--turn on oled panel
};

//extern unsigned char Hzk[][];


void OLED_WR_CMD_DMA(uint8_t* CMD_Data)
{
	HAL_I2C_Mem_Write(&hi2c1 ,0x78,0x00,I2C_MEMADD_SIZE_8BIT,CMD_Data,1,0x100);
}
void OLED_WR_DATA_DMA(uint8_t Data)
{
	HAL_I2C_Mem_Write(&hi2c1,0x78,0x40,I2C_MEMADD_SIZE_8BIT,&Data,1,0x100);
}

void OLED_Buffer_DMA_ON(){
    HAL_I2C_Mem_Write_DMA(&hi2c1,0x78,0x40,I2C_MEMADD_SIZE_8BIT,(uint8_t*)OLED_Buffer,1024);
}

void OLED_Buffer_Write(unsigned int Addr, unsigned char dat)
{
	OLED_Buffer[0][Addr] = dat;
}

//---写成两部分是为了方便L组调用---//
unsigned int OLED_Buffer_ConfigAddr_1(unsigned int Addr)
{
	return Addr+OLED_Offset;
}
unsigned int OLED_Buffer_ConfigAddr_2(unsigned int Addr)
{
	if(Addr>=1024)return Addr-1024;
	else return Addr;
}

//---show char Process---//
void OLED_Buffer_Input(int Addr,unsigned char* pchr)
{
	unsigned char i,ID;
	ID=OLED_DMA_LookID(pchr);
	//---宏定义是否需要位置矫正---//
	#if (MyOLED == 1)
		Addr = OLED_Buffer_ConfigAddr_1(Addr);
		Addr = OLED_Buffer_ConfigAddr_2(Addr);
	#endif
	//---Input str_H---//
	for(i=0;i<8;i++)OLED_Buffer_Write(Addr+i,*(OLED_DMA_CharPtr+ID*16+i));
	Addr += 128;
	#if (MyOLED == 1)
		Addr = OLED_Buffer_ConfigAddr_2(Addr);
	#endif
	//---Input str_L---//
	for(i=0;i<8;i++)OLED_Buffer_Write(Addr+i,*(OLED_DMA_CharPtr+ID*16+8+i));
}
//---Way_1:show char---//
unsigned char OLED_Buffer_Inchar_ser(unsigned char ser,unsigned char* pchr)
{
	//---ser >>> Addr---//
	if(ser>=64)return 0;
	else {
		unsigned int Addr;
		if(ser<16)Addr = ser*8;
		else if(ser<32)Addr = (ser-16*1)*8+256*1;
		else if(ser<48)Addr = (ser-16*2)*8+256*2;
		else Addr = (ser-16*3)*8+256*3;
		OLED_Buffer_Input(Addr,pchr);
		return 1;
	}
}
//---Way_2:show char---//
unsigned char OLED_Buffer_Inchar_xy(unsigned char x,unsigned char y,unsigned char* pchr)
{	
	//---x,y >>> Addr---//
	if(x>=16||y>=4)return 0;
	else {
		unsigned int Addr;
		Addr = y*256+x*8;
		OLED_Buffer_Input(Addr,pchr);
	}
  return 1;
}
//---show string---//
unsigned char OLED_Buffer_Instr(unsigned char ser,unsigned char* pstr,unsigned char size){
	unsigned char i;
	if(ser+size>=64)return 0;
	for(i=0;i<size;i++){
		OLED_Buffer_Inchar_ser(ser+i,pstr+i);
	}
	return 1;
}

//初始化oled屏幕
void OLED_DMA_Init(void)
{ 	
	unsigned char len;
	unsigned int i;
	len = sizeof(InitCMD)/sizeof(unsigned char);
	//memcpy(OLED_Buffer[0],Hzk_DMA[0],352);
	//----Init Buffer---//
	for(i=0;i<1024;i++)OLED_Buffer_Write(i,InitDat);
	HAL_Delay(200);
	HAL_I2C_Mem_Write(&hi2c1 ,0x78,0x00,I2C_MEMADD_SIZE_8BIT,(uint8_t*)InitCMD,len,0x05);
}

unsigned char* Str8_float(float f)
{
	unsigned char r,i,num[8];
	unsigned int l;
	unsigned char* p;
	r = '?';
	p=&r;
	//判断浮点数是否在可处理范围
	if(f>1000.0|f<0)return p;
	//扩大1000倍转为整形，取整数部分(保留了原数的小数点后三位)
	l=(unsigned int)f*1000;
	for(i=0;i<8;i++){
		if(i==4){num[3]='.';continue;}
		//取个位
		num[7-i]=l%10+'0';
		l/=10;
	}
	p=num;
	return p; 
}


